package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.MobileOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 预约控制器业务层实现类
 *
 * @author szh
 * @since 2022/11/24 13:08
 */
@Slf4j
@Service
public class MobileOrderServiceImpl implements MobileOrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    //会员dao层
    @Autowired
    private MemberDao memberDao;

    //体检预约信息dao层
    @Autowired
    private OrderDao orderDao;


    /**
     * 提交预约信息
     *
     * @param map
     */
    @Override
    public Result submit(Map<String, String> map) throws ParseException {


        //获取体检日期
        String orderDate = map.get("orderDate");
        //不填写体检日期校验
        if (null == orderDate) {
            log.info("请输入体检日期");
            return new Result(false, "请输入体检日期");
            //throw new BusinessRuntimeException("请输入体检日期");
        }

        //检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置不能预约
        //根据体检日期查询查询预约设置表
        OrderSetting orderSetting = this.orderSettingDao.findByOrderDate(orderDate);

        if(null==orderSetting){
            //指定日期没有进行预约设置无法完成预约
            return new Result(false,MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }


        //校验该体检预约是否约满
        int number = orderSetting.getNumber(); //可预约人数
        int reservations = orderSetting.getReservations();//已经预约人数
        if (reservations >= number) {
            //已经约满无法预约
            return  new Result(false,MessageConst.ORDER_FULL);
        }


        //会员同一天不能重复预约
        //获取用户页面的手机号
        String telephone = map.get("telephone");

        //根据电话号查询会员信息
        Member member = this.memberDao.findByTelephone(telephone);

        //查询到同一个用户
        if (member != null) {
            //判断是否重复预约
            Integer memberId = member.getId(); //会员ID

            Integer setmealId = Integer.parseInt(map.get("setmealId"));//套餐id

            //根据条件进行查询  -->条件会员ID、体检日期、套餐id
            List<Order> orderList = this.orderDao.findByOrder(memberId,orderDate,setmealId);
            if (orderList != null && orderList.size() > 0) {
                return new Result(false, MessageConst.HAS_ORDERED);
            }
        }


        //预约成功，更新当日预约人数
        Order order = new Order();
        order.setMemberId(member.getId());  //设置会员id
        order.setOrderDate(new SimpleDateFormat("yyyy-MM-dd").parse(orderDate)); //预约日期
        order.setOrderType(map.get("orderType")); //设置预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO); //设置预约状态
        order.setSetmealId(Integer.parseInt(map.get("setmealId"))); //设置体检套餐id

        //添加预约信息
        this.orderDao.add(order);

        //更新当日预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);

        //根据预约日期修改预约人数
        this.orderSettingDao.updateOrderSetting(orderSetting);

        return new Result(true,MessageConst.ORDER_SUCCESS,order);
    }
}
